/*class profilePage
 *
 *since version: 0.5
 *
 *page instace of profilePage
 *
 *
 *
 */
//instance
var profilePage = null;
function ProfilePage()
{
    this.innerHTML = this.defaultHTML;
    this.body = new DOMObject("body");
    this.profileUsername = new DOMObject("profileUsername");
    this.profileMailbox = new DOMObject("profileMailbox");
    this.profileRealname = new DOMObject("profileRealname");
    this.profileCurrentCheckoutList = new DOMObject("profileCurrentCheckoutList");
    this.profileCheckoutHistory = new DOMObject("profileCheckoutHistory");
}
ProfilePage.prototype.defaultHTML = "loading..."
ProfilePage.prototype.load = function()
{
    var This = this;
    var xhr = new XMLHttpRequest();
    xhr.open("GET",'profilePage.ajax',true)
    xhr.onreadystatechange = function()
    {
	if(xhr.readyState ==4)
	{
	    This.innerHTML = xhr.responseText;
	    //add 404 handler at next version
	}
    }
    xhr.send();
}
ProfilePage.prototype.show = function()
{
    this.body.$().innerHTML = this.innerHTML;
    if(this.innerHTML == this.defaultInnerHTML)
    {
	this.load();
	setTimeout(function(){profilePage.show()},1000);
	return false;
    }
    this.manageStatus();
    this.initInteraction();
}
ProfilePage.prototype.manageStatus = function()
{
    //fill client info
    var clientInfo = userManager.getClientInfo();
    if(!clientInfo)
    {
	Show("Problem Connecting Server.Retry...");
	setTimeout(manageStatus);
    }
    this.profileUsername.$().innerHTML = clientInfo.username;
    this.profileMailbox.$().innerHTML = clientInfo.mailbox;
    this.profileRealname.$().innerHTML = clientInfo.realname;
    //add currentcheckout
    var html = "";
    this.profileCurrentCheckoutList.$().innerHTML = this.genCurrentCheckoutList();
    //add checkoutHisoty
    this.profileCheckoutHistory.$().innerHTML = this.genCheckoutHistoryList();
}
ProfilePage.prototype.genCurrentCheckoutList = function()
{
    var jsonRecords = userManager.getCurrentCheckout();
    if(!jsonRecords)
    {
	return null;
    }
    var html = "";
    if(jsonRecords.length==0)
    {
	html = "<h2 class='noResult'>看来这里没什么东西呢</h2>";
    }
    for(var i = 0;i<jsonRecords.length;i++)
    {
	var strtemp = this.genCurrentCheckoutItem(jsonRecords[i]);
	if(i==0)
	{
	    strtemp = strtemp.replace("notFirst","");
	}
	html+= strtemp;
    }
    return html;
}
ProfilePage.prototype.genCurrentCheckoutItem = function(jsonRecord)
{
    var strItem = "<div class='listItem notFirst'> <div class='upperTab'> <div class='left'> <div class='right'> <div class='center'> <h1 class='bookName'>somename</h1> </div> </div> </div> </div> <div class='upDownExpandBody largeRoundBody'> <div class='up'> <div class='down'> <div class='center'> <div class='listContent'> <span class='attrName'>bookCode:</span> <span>something</span> <span class='attrName'>bookLocation:</span> <span>somewhere</span> <span class='attrName'>bookPublisher:</span> <span>somebody</span> <div class='actions'> <div class='leftRightExpandBody orangeButton clickable return_this' id='someID' code='S006'><div class='left'><div class='right'><div class='center'> <span>Return Me!</span> </div></div></div></div> </div> <!-- end actions --> </div> <!-- end listContent --> </div> </div> </div> </div> </div>";
    var temp = strItem;
    temp = temp.replace("somename",jsonRecord.name);
    temp = temp.replace("something",jsonRecord.ID);
    temp = temp.replace("sometime",jsonRecord.checkout_time);
    temp = temp.replace("someID",jsonRecord.ID);
    return temp;
}
ProfilePage.prototype.genCheckoutHistoryList = function()
{
    var jsonRecords = userManager.getCheckoutHistory();
    if(!jsonRecords)
    {
	html = "<h2 class='noResult'>看来这里没什么东西呢</h2>";
	return null;
    }
    var html = ""
    if(jsonRecords.length==0)
    {
	html = "<h2 class='noResult'>看来这里没什么东西呢</h2>";
    }
    for(var i = 0; i < jsonRecords.length;i++)
    {
	html+=this.genCheckoutHistoryItem(jsonRecords[i]);
	if(i==0)
	{
	    html = html.replace("notFirst","");
	}
    }
    return html;
}

ProfilePage.prototype.genCheckoutHistoryItem = function(jsonRecord)
{
    var strHistory =  "<div class='listItem notFirst'> <div class='upperTab'> <div class='left'> <div class='right'> <div class='center'> <h1 class='bookName'>somename</h1> </div> </div> </div> </div> <div class='upDownExpandBody largeRoundBody'> <div class='up'> <div class='down'> <div class='center'> <div class='listContent'> <span class='attrName'>bookCode:</span> <span>something</span> <span class='attrName'>bookLocation:</span> <span>somewhere</span> <span class='attrName'>bookPublisher:</span> <span>somebody</span><span class='attrName'>checkoutTime:</span> <span>sometime</span><span class='attrName'>returnTime:</span> <span>thantime</span> <!--div class='actions'> <div class='leftRightExpandBody orangeButton clickable is_available' id='someID' code='S006'><div class='left'><div class='right'><div class='center'> <span>Return Me!</span> </div></div></div></div> </div--> <!-- end actions --> </div> <!-- end listContent --> </div> </div> </div> </div> </div>";
    var temp = strHistory;
    temp = temp.replace("somename",jsonRecord.name);
    temp = temp.replace("something",jsonRecord.ID);
    temp = temp.replace("sometime",jsonRecord.checkout_time);
    temp = temp.replace("thantime",jsonRecord.return_time);
    temp = temp.replace("someID",jsonRecord.ID)
    
    //checkout from history list at next version
    //if(jsonRecord.is_available.toString()!="1")
    //{
    //	temp = temp.replace("is_available","");
    //}
    //else
    //{
    //	temp = temp.replace("Return Me!",再借来看看)
    //}
    return temp;
}
ProfilePage.prototype.initInteraction = function()
{
    var toReturn = document.getElementsByClassName("return_this");
    
    //** enable checkout from history at next version by 
    //uncomment it after php JSONapi added is_available attr
    //var canCheckout = document.getElementsByClassName("is_available");
    //for(var i =0;i<canCheckout.length;i++)
    //{
    //	canCheckout[i].onclick = searcher.__checkoutFactory(canCheckout[i].id);
    //}
    

    //here is a *PROBLEM* how to determine if the datas have complete load
    //to be solved at next version
    
    if(toReturn.length==0)
    {
    	setTimeout(function(){profilePage.initInteraction()},1000);
    	return false;
    }
    for(var i = 0;i < toReturn.length;i++)
    {
	toReturn[i].onclick = searcher.__returnFactory(toReturn[i].id);
    }
    return true;
}